﻿using System;
using System.Collections.Generic;
using System.Text;


using TRPOP.CMS.Model;
using TRPOP.CMS.DALFactory;
using TRPOP.Common;


namespace TRPOP.CMS.Business
{
    public class AdvscontentManage
    {

        private IAdvscontentDAL Idal;
        public AdvscontentManage()
        {
            Idal = DataAccess.CreateadvscontentDAL();
        }

        #region 保存 Save
        ///	<summary>
        ///	保存
        ///	</summary>
        ///	<param name="AdvscontentInfo"></param>
        ///	<returns>EnumSubmitResult</returns>
        public EnumSubmitResult Save(AdvscontentInfo info)
        {
            return Idal.Save(info);
        }

        #endregion

        #region 获取实体 GetInfoById
        ///	<summary>
        ///	获取实体
        ///	</summary>
        ///	<param name="adID"></param>
        ///	<returns>AdvscontentInfo</returns>
        public AdvscontentInfo GetInfoById(int id)
        {
            return Idal.GetInfoById(id);
        }
        #endregion

        #region 删除 DeleteInfoById
        ///	<summary>
        ///	删除
        ///	</summary>
        ///	<param name="id"></param>
        ///	<returns>EnumSubmitResult</returns>
        public EnumSubmitResult DeleteInfoById(int id)
        {
            string m_ConfigTypeKey = "Advertisement";
            string m_ParamKey = "Advertisement";
            AdvscontentInfo info = GetInfoById(id);
            if (info != null)
            {
                if (info.ShowModel == 0)
                {
                    try
                    {
                        string strRoot = SYSConfig.GetVarValue(m_ParamKey, m_ConfigTypeKey)[3].ToString();
                        string path = "";
                        if (strRoot.EndsWith("/"))
                        {
                            path = strRoot + info.PageName + ".js";
                        }
                        else
                        {
                            path = strRoot + "/" + info.PageName + ".js";
                        }
                        if (info.ShowModel == 0)
                        {
                            FileUtil.DeleteFile(path, false, "");
                        }
                    }
                    catch { }
                }
            }
            return Idal.DeleteInfoByIds(id.ToString());
        }
        #endregion

        #region 删除 DeleteInfoByIds
        ///	<summary>
        ///	删除
        ///	</summary>
        ///	<param name="ids"></param>
        ///	<returns>EnumSubmitResult</returns>
        public EnumSubmitResult DeleteInfoByIds(string ids)
        {
            return Idal.DeleteInfoByIds(ids);
        }
        #endregion

        #region 自定义列表（无分页） GetListByCondition
        ///	<summary>
        ///	自定义列表（无分页）
        ///	</summary>
        ///	<param name="topn">前N条，为0时表示所有符合条件的</param>
        ///	<param name="condition">自定义条件，要以And开头</param>
        ///	<param name="orderby">排序，不需要Order by </param>
        ///	<returns>List<AdvscontentInfo></returns>
        public List<AdvscontentInfo> GetListByCondition(int topn, string condition, string orderby)
        {
            return Idal.GetListByCondition(topn, condition, orderby);
        }
        #endregion

        #region 自定义列表（分页） GetListByPager
        ///	<summary>
        ///	自定义列表（分页）
        ///	</summary>
        ///	<returns>List<AdvscontentInfo></returns>
        public List<AdvscontentInfo> GetListByPager(int pageNumber, int pageSize, string condition, string orderby, out int recordCount)
        {
            return Idal.GetListByPager(pageNumber, pageSize, condition, orderby, out  recordCount);
        }
        #endregion


        #region 获取最大Id
        /// <summary>
        /// 获取最大Id
        /// </summary>
        /// <returns></returns>
        public int GetMaxId()
        {
            return Idal.GetMaxId();
        }
        #endregion

        #region 通过PageName获取信息
        /// <summary>
        /// 通过PageName获取信息
        /// </summary>
        /// <param name="pageName"></param>
        /// <returns></returns>
        public AdvscontentInfo GetInfoByPageName(string pageName)
        {
            pageName = pageName.Replace("'","");
            return Idal.GetInfoByPageName(pageName);
        }
        #endregion


        #region 页面更新时用到的
        /// <summary>
        /// 获取过期 或无效 的JS
        /// </summary>
        /// <returns></returns>
        public List<AdvscontentInfo> GetNotValidList()
        {
            return Idal.GetListByCondition(0, " and  (  MaturityTime < getdate()  or IsValid='false' ) ", "");
        }
        /// <summary>
        /// 获取有效的JS 并且是静态的
        /// </summary>
        /// <returns></returns>
        public List<AdvscontentInfo> GetValidAndStaticList()
        {
            return Idal.GetListByCondition(0, " and MaturityTime >= getdate()  and IsValid='true'  and ShowModel=0  ", "");
        }
        /// <summary>
        /// 更新js文件
        /// </summary>
        /// <param name="intId"></param>
        public void UpdateJSFile(int intId)
        {
            AdvscontentInfo mAdvscontentInfo = GetInfoById(intId);
            if (mAdvscontentInfo == null)
                return;
            string m_ConfigTypeKey = "Advertisement";
            string m_ParamKey = "Advertisement";
            string strRoot = SYSConfig.GetVarValue(m_ParamKey, m_ConfigTypeKey)[3].ToString();
            string path = "";
            if (strRoot.EndsWith("/"))
            {
                path = strRoot + mAdvscontentInfo.PageName + ".js";
            }
            else
            {
                path = strRoot + "/" + mAdvscontentInfo.PageName + ".js";
            }
            if (mAdvscontentInfo.ShowModel == 0)
            {
                JsUtil.WriteJSFile(mAdvscontentInfo.Detial, Common.Util.GetMapPath(path));
            }
            else
            {
                FileUtil.DelFile(Common.Util.GetMapPath(path));
            }
        }

        public void UpdateJSFile(AdvscontentInfo mAdvscontentInfo)
        {
            if (mAdvscontentInfo == null)
                return;
            string m_ConfigTypeKey = "Advertisement";
            string m_ParamKey = "Advertisement";
            string strRoot = SYSConfig.GetVarValue(m_ParamKey, m_ConfigTypeKey)[3].ToString();     
            string path = "";
            if (strRoot.EndsWith("/"))
            {
                path = strRoot + mAdvscontentInfo.PageName + ".js";
            }
            else
            {
                path = strRoot + "/" + mAdvscontentInfo.PageName + ".js";
            }

            UpdateIMGUrl(mAdvscontentInfo);

            if (mAdvscontentInfo.ShowModel == 0)
            {
                JsUtil.WriteJSFile(mAdvscontentInfo.Detial, Common.Util.GetMapPath(path));
            }
            else
            {
                FileUtil.DelFile(Common.Util.GetMapPath(path));
            }
        }
        /// <summary>
        /// 删除js文件
        /// </summary>
        /// <param name="id"></param>
        public void DeleteJSFile(int id)
        {
            string m_ConfigTypeKey = "Advertisement";
            string m_ParamKey = "Advertisement";
            AdvscontentInfo info = GetInfoById(id);
            if (info != null)
            {
                if (info.ShowModel == 0)
                {
                    try
                    {
                        string strRoot = SYSConfig.GetVarValue(m_ParamKey, m_ConfigTypeKey)[3].ToString();
                        string path = "";
                        if (strRoot.EndsWith("/"))
                        {
                            path = strRoot + info.PageName + ".js";
                        }
                        else
                        {
                            path = strRoot + "/" + info.PageName + ".js";
                        }
                        if (info.ShowModel == 0)
                        {
                            FileUtil.DeleteFile(path, false, "");
                        }
                    }
                    catch{ }
                }
            }
        }

        /// <summary>
        /// 保存信息后二次更新信息实体
        /// </summary>
        /// <param name="mAdvscontentInfo"></param>
        /// <param name="blnSave">是否保存</param>
        public void UpdateAfterSave(AdvscontentInfo oAdvscontentInfo,bool blnSave)
        {
            string strUrl = SYSConfig.GetVarValue("Advertisement", "Advertisement")[7].ToString().Trim();
            oAdvscontentInfo.Detial = oAdvscontentInfo.Detial.Replace("[$AdvertisementSkip]",
                                                                strUrl +
                                                                (strUrl.Contains("?") ? "&" : "?") +
                                                                "id=" + oAdvscontentInfo.AdID.ToString()
                                        );
            if (blnSave)
                Save(oAdvscontentInfo);
        }
        /// <summary>
        /// 更新图片URL(不保存)
        /// </summary>
        /// <param name="oAdvscontentInfo"></param>
        public void UpdateIMGUrl(AdvscontentInfo oAdvscontentInfo)
        {
            string strPSNUrl = SYSConfig.GetVarValue("Advertisement", "Advertisement")[8].ToString().Trim();
            string strPSN = SYSConfig.GetVarValue("Advertisement", "Advertisement")[6].ToString().Trim();
            PSNManage mPsn=new PSNManage();
           
            string strDetail = oAdvscontentInfo.Detial;

             CommonRegex.UrlAddForCkfinder(mPsn.GetPSNURLByPSNUrl(strPSNUrl), mPsn.GetPSNPathByPSN(strPSN), ref strDetail);
             oAdvscontentInfo.Detial = strDetail;

        }
        #endregion

    }
}
